package avicit.bdp.dds.alert.runner;

import avicit.bdp.dds.common.plugin.PluginManager;
import avicit.bdp.dds.dao.AlertDao;
import avicit.bdp.dds.dao.entity.Alert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * alert sender
 */
public class AlertSender {

    private static final Logger logger = LoggerFactory.getLogger(AlertSender.class);

    private List<Alert> alertList;
    private AlertDao alertDao;
    private PluginManager pluginManager;

    public AlertSender() {
    }

    public AlertSender(List<Alert> alertList, AlertDao alertDao, PluginManager pluginManager) {
        super();
        this.alertList = alertList;
        this.alertDao = alertDao;
        this.pluginManager = pluginManager;
    }

    public void run() {
//        List<User> users;
//        Map<String, Object> retMaps = null;
//        for (Alert alert : alertList) {
//            users = alertDao.listUserByAlertgroupId(alert.getAlertGroupId());
//
//            // receiving group list
//            List<String> receviersList = new ArrayList<>();
//            for (User user : users) {
//                receviersList.add(user.getEmail());
//            }
//
//            AlertData alertData = new AlertData();
//            alertData.setId(alert.getId())
//                    .setAlertGroupId(alert.getAlertGroupId())
//                    .setContent(alert.getContent())
//                    .setLog(alert.getLog())
//                    .setReceivers(alert.getReceivers())
//                    .setReceiversCc(alert.getReceiversCc())
//                    .setShowType(alert.getShowType().getDescp())
//                    .setTitle(alert.getTitle());
//
//            AlertInfo alertInfo = new AlertInfo();
//            alertInfo.setAlertData(alertData);
//
//            alertInfo.addProp("receivers", receviersList);
//
//            AlertPlugin emailPlugin = pluginManager.findOne(Constants.PLUGIN_DEFAULT_EMAIL);
//            retMaps = emailPlugin.process(alertInfo);
//
//            if (retMaps == null) {
//                alertDao.updateAlert(AlertStatus.EXECUTION_FAILURE, "alert send error", alert.getId());
//                logger.info("alert send error : return value is null");
//            } else if (!Boolean.parseBoolean(String.valueOf(retMaps.get(Constants.STATUS)))) {
//                alertDao.updateAlert(AlertStatus.EXECUTION_FAILURE, String.valueOf(retMaps.get(Constants.MESSAGE)), alert.getId());
//                logger.info("alert send error : {}", retMaps.get(Constants.MESSAGE));
//            } else {
//                alertDao.updateAlert(AlertStatus.EXECUTION_SUCCESS, (String) retMaps.get(Constants.MESSAGE), alert.getId());
//                logger.info("alert send success");
//            }
//        }

    }

}
